package com.hcinfos.gsoo.ui;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Common;
import com.hcinfos.gsoo.config.Urls;
import com.hcinfos.gsoo.db.NewDBUtil;
import com.hcinfos.gsoo.enity.User;
import com.hcinfos.gsoo.https.HttpClientUtil;
import com.hcinfos.gsoo.interaze.DialogCallBack;
import com.hcinfos.gsoo.util.DialogUtil;
import com.hcinfos.gsoo.util.LogUtil;
import com.hcinfos.gsoo.util.SharePreferenceUtil;

public class UserInfoActivity extends Activity implements OnClickListener {
	private TextView reg_tv;
	private ImageButton left_ib;
	private TextView center_tv;
	private ImageView right_img;
	private SharePreferenceUtil util;
	private Context context;

	private TextView userName_tv;
	private TextView email_tv;
	private TextView integral_tv;
	private TextView level_tv;
	private TextView experience_tv;
	private SharePreferenceUtil imUtil;
	private NewDBUtil newDBUtil;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		Common.PROPORTION = width / 320f;
		Common.WIDTH = width;
		util = new SharePreferenceUtil(this, "setting");
		context = this;
		newDBUtil = NewDBUtil.newInstance(this);
		setContentView(R.layout.activity_user_info);
		initRes();
	}

	private void initRes() {
		left_ib = (ImageButton) findViewById(R.id.left_ib);
		reg_tv = (TextView) findViewById(R.id.reg_tv);
		reg_tv.setVisibility(View.GONE);
		center_tv = (TextView) findViewById(R.id.center_tv);
		center_tv.setText("个人中心");
		right_img = (ImageView) findViewById(R.id.right_img);
		right_img.setVisibility(View.VISIBLE);
		right_img.setImageResource(R.drawable.logout_account_u);
		left_ib.setOnClickListener(this);
		right_img.setImageResource(R.drawable.logout_account_u);
		right_img.setOnClickListener(this);

		userName_tv = (TextView) findViewById(R.id.userName_tv);
		email_tv = (TextView) findViewById(R.id.email_tv);
		integral_tv = (TextView) findViewById(R.id.integral_tv);
		level_tv = (TextView) findViewById(R.id.level_tv);
		experience_tv = (TextView) findViewById(R.id.experience_tv);
	}

	private User user;

	@Override
	protected void onResume() {
		super.onResume();
		Object obj = util.loadObject("user");
		if (obj != null) {
			user = (User) obj;
			center_tv.setText("详细资料");
			userName_tv.setText("用户名:  " + user.getUsername());
			email_tv.setText("邮箱:  " + user.getEmail() == null ? "邮  箱: 暂无"
					: "邮  箱:" + user.getEmail());
			integral_tv.setText("积分:  " + user.getGold());
			level_tv.setText("等级:  " + user.getLevel());
			experience_tv.setText("经验值:  " + user.getExp());
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_ib:
			finish();
			break;
		case R.id.right_img:
			DialogUtil.createCallLoginDialog(UserInfoActivity.this, "你确定要注销账户",
					new DialogCallBack() {

						@Override
						public void viewClick() {

						}

						@Override
						public void ok() {
							logOut();
						}

						@Override
						public void init(Object obj) {

						}

						@Override
						public void cancel() {

						}
					}, "确定", "取消");
			break;
		}

	}

	public final int LOGOUT_SUCCESS = 0x1120;
	public final int LOGOUT_FAIL = LOGOUT_SUCCESS + 1;

	/**
	 * 注销
	 */
	private void logOut() {

		new Thread(new Runnable() {

			@Override
			public void run() {

				try {
					String result = HttpClientUtil.getFromWebByHttpClient(
							context, Urls.USER_LOGOUT);
					if (result == null) {
						Message msg = handler.obtainMessage();
						msg.what = LOGOUT_FAIL;
						handler.sendMessage(msg);
					} else {

						JSONObject obj = new JSONObject(result);
						int state = obj.getInt("state");
						String message = obj.getString("message");
						if (state == 200) {
							Message msg = handler.obtainMessage();
							msg.what = LOGOUT_SUCCESS;
							handler.sendMessage(msg);
						} else {
							Message msg = handler.obtainMessage();
							msg.what = LOGOUT_FAIL;
							msg.obj = message;
							handler.sendMessage(msg);
						}
					}
					LogUtil.d("wxf", "result:" + result);
				} catch (Exception e) {
					Message msg = handler.obtainMessage();
					msg.what = LOGOUT_FAIL;
					handler.sendMessage(msg);
					LogUtil.d("wxf", "e:" + e.toString());
				}
			}
		}).start();
	}

	Handler handler = new Handler() {

		public void handleMessage(Message msg) {

			switch (msg.what) {
			case LOGOUT_SUCCESS:
				imUtil.removeAllKey();
				imUtil.removeKey("userInfo");
				util.removeKey("user");
				util.removeKey("userName");
				util.removeKey("password");
				util.removeKey("email");
				Intent intent = new Intent(UserInfoActivity.this,
						HomeActivity.class);
				startActivity(intent);
				finish();
				Toast.makeText(context, "注销成功", 500).show();
				break;
			case LOGOUT_FAIL:
				if (msg.obj != null) {
					Toast.makeText(context, msg.obj.toString(), 500).show();
				}
				break;
			}
		};
	};
}
